//
//  YbrSandBox.m
//  WifiForBox
//
//  Created by xinle on 15/8/17.
//  Copyright (c) 2015年 yinbaner. All rights reserved.
//

#import "YbrSandBox.h"

@implementation YbrSandBox

/**
 * 创建文件沙盒,包括图片路径,音乐路径,以及json存放的路径
 * @param 注意这些路径的使用,要百分百正确
 *
 */
+(void)createFile
{
    NSFileManager *fileManager  =   [[NSFileManager alloc] init];
    NSString *pathDocuments     =   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *createPath        =   [NSString stringWithFormat:@"%@/Image", pathDocuments];
    NSString *createDir           =   [NSString stringWithFormat:@"%@/Music", pathDocuments];
    NSString *createJson        =   [NSString stringWithFormat:@"%@/Json", pathDocuments];
    NSString *initdic                = [NSString stringWithFormat:@"%@/Dic",pathDocuments];
    
    // 判断文件夹是否存在，如果不存在，则创建
    if (![[NSFileManager defaultManager] fileExistsAtPath:initdic]) {
        [fileManager createDirectoryAtPath:initdic withIntermediateDirectories:YES attributes:nil error:nil];
    } else {
        NSLog(@"initdic is exists.");
    }
    if (![[NSFileManager defaultManager] fileExistsAtPath:createPath]) {
        [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
    } else {
        NSLog(@"Image is exists.");
    }
    if (![[NSFileManager defaultManager] fileExistsAtPath:createDir]) {
        [fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
    } else {
        NSLog(@"Music is exists.");
    }
    if (![[NSFileManager defaultManager] fileExistsAtPath:createJson]) {
        [fileManager createDirectoryAtPath:createJson withIntermediateDirectories:YES attributes:nil error:nil];
    } else {
        NSLog(@"Json is exists.");
    }
}

+(NSString *)dicAppendFilePath:(NSString *)dicName
{
    NSString *dicUrl = [[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Dic"] stringByAppendingPathComponent:dicName];
    return dicUrl;
}

/**
 * IOS系统中,图片存放的路径
 * @params  imageName   图片名称
 * @return  imgUrl      图片路径
 *
 */
+(NSString *)imageAppendFilePath:(NSString *)imageName
{
    NSString *imgUrl = [[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Image"] stringByAppendingPathComponent:imageName];
    return imgUrl;
}

/**
 * IOS系统中,音频存放的路径
 * @params audioName    音频的名称
 * @return audioUrl     音频的路径
 *
 */
+(NSString *)audioAppendFilePath:(NSString *)audioName
{
    NSString *audioUrl = [[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Music"] stringByAppendingPathComponent:audioName];
    return audioUrl;
}

/**
 * IOS系统中，下载文件的名称
 * @params  downfileName    下载文件的名称
 * @return  path            下载文件的路径
 *
 */
+(NSString *)downAppendFilePath:(NSString *)downFileName
{
    NSString *path = [[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"AppLunch"]stringByAppendingPathComponent:downFileName];
    return path;
}

/**
 * IOS系统中,下载Json文件的名称
 * @params  downFileName    下载的json文件
 * @return  path            下载的json的文件路径
 *
 */
+(NSString *)downJsonAppendFilePath:(NSString *)downFileName
{
    NSString *path = [[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"Json"]stringByAppendingPathComponent:downFileName];
    return path;
}


/**
 * 删除Json文件
 * @params  name    Json文件路径
 * @return  bool    删除结果
 *
 */
+(BOOL)deleteJsonFromName:(NSString *)name
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    //文件名
    NSString *uniquePath=[[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"Json"]stringByAppendingPathComponent:name];
    BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
    if (blDele) {
        return YES;
    }else {
        return NO;
    }

}


/**
 * 删除视频
 * @params  name    视频名称
 * @return  bool    删除结果
 *
 */
+(BOOL)deleteMVFromName:(NSString *)name
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    //文件名
    NSString *uniquePath=[[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"AppLunch"]stringByAppendingPathComponent:name];
    BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
    if (blDele) {
        return YES;
    }else {
        return NO;
    }
}


/**
 * 保存图片至沙盒
 * @params   imageName    图片名称
 * @return   void
 *
 */
#pragma mark - 保存图片至沙盒
+(void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData  = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录
    NSString *fullPath = [[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Image"] stringByAppendingPathComponent:imageName];
    NSLog(@"--------用户头像的图片-----%@",fullPath);
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}


/**
 * 删除图片
 * @params  name    图片名称
 * @return  bool    删除结果
 *
 */
+(BOOL)deleteFromName:(NSString *)name
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    //文件名
    NSString *uniquePath  = [[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"Image"] stringByAppendingPathComponent:name];
    BOOL blHave           = [[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
    if (!blHave) {
        return NO;
    }else {
        BOOL blDele       = [fileManager removeItemAtPath:uniquePath error:nil];
        if (blDele) {
            return YES;
        }else {
            return NO;
        }
    }
}

/**
 * 删除音频
 * @params  name    音频名称
 * @return  bool    删除结果
 *
 */
+(BOOL)deleteAudioFromName:(NSString *)name
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    //文件名
    NSString *uniquePath=[[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"Music"] stringByAppendingPathComponent:name];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
    if (!blHave) {
        return NO;
    }else {
        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
        if (blDele) {
            return YES;
        }else {
            return NO;
        }
    }

}

/**
 * 读取图片
 * @params  imageName   图片名称
 * @return  readImage   返回图片对象
 *
 */
+(UIImage *)readImage:(NSString *)imageName
{
    NSString *fullPath = [[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Image"] stringByAppendingPathComponent:imageName];
    UIImage *readImage = [[UIImage alloc]initWithContentsOfFile:fullPath];
    return readImage;
}



/////////////// 保存网络图片 //////////////////////////////





@end




